package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.util.Date;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import vrp.PMF;
import vrp.Carrier;

public class CarrierEditServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	
        long keyid = Integer.parseInt(req.getParameter("key"));
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String postalCode = req.getParameter("postalCode");
        String contactNo = req.getParameter("contactNo");
        int status = Integer.parseInt(req.getParameter("status"));
        
        Key key = KeyFactory.createKey(Carrier.class.getSimpleName(), keyid);
        Carrier carrier = pm.getObjectById(Carrier.class, key);
        
        try {
            carrier.setName(name);
            carrier.setAddress(address);
            carrier.setPostalCode(postalCode);
            carrier.setContactNo(contactNo);
            carrier.setStatus(status);
            carrier.setModifiedAt(new Date());
            pm.makePersistent(carrier);
        } finally {
            pm.close();
        }
        resp.sendRedirect("/carriers.jsp");
    }
}
